package com.bw.controller;

import com.alibaba.fastjson2.JSON;
import com.bw.domain.TbMenu;
import com.bw.domain.User;
import com.bw.service.MenuService;
import com.bw.utils.MenuUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * @author :Beyond🎸🎸
 * @date : 2025-02-28 19:28:07
 * @description Rock and Roll
 **/
@Controller
public class MainController {

    @Autowired
    MenuService menuService;

    @Autowired
    StringRedisTemplate stringRedisTemplate;

    @RequestMapping("main")
    public String main(HttpSession httpSession, Model model){
    User user = (User) httpSession.getAttribute("userInfo");
//  根据当前登陆人的信息查询其拥有的菜单
    List<TbMenu> menuList = menuService.list(user.getId());
//  形成树形结构
    List<TbMenu> list = MenuUtil.treeMenus(menuList,null);

    model.addAttribute("list",list);
    stringRedisTemplate.opsForValue().set("UserMenu", JSON.toJSONString(list));
        return "main";
    }

}
